package com.system.service.comment.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.constant.HTTPStatus;
import com.system.entity.comment.Comment;
import com.system.mapper.comment.CommentMapper;
import com.system.response.PageDto;
import com.system.response.Result;
import com.system.response.comment.vo.CommentVo;
import com.system.service.comment.CommentService;
import com.system.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname CommentServiceImpl
 * @Date 2023/10/8 9:20
 * @Created ZFC
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 分页查询评论，树形结构展示
     *
     * @param pageDto
     * @return
     */
    @Override
    public Result limit(PageDto pageDto) {
        //TODO:分页功能暂时没有完成。
        if (pageDto.getPageSize()==null || pageDto.getPageSize()<0) pageDto.setPageSize(1);
        if (pageDto.getPageNum()==null || pageDto.getPageNum()<10000) pageDto.setPageNum(10000);

        
        //查询所有评论,倒叙查询
        List<Comment> commentList = commentMapper.selectList(new LambdaQueryWrapper<Comment>().orderByDesc(Comment::getCreateDate));

        //构建树形评论结构
        List<CommentVo> commentVoList = new ArrayList<>();

        //判断评论，如果parentId为-1，代表是首条评论
        commentList.forEach(comment -> {
            if (comment.getParentId() == -1){
                //将该条评论添加到树形结构中
                CommentVo commentVo = new CommentVo();
                BeanUtils.copyProperties(comment,commentVo);
                commentVoList.add(commentVo);
            }
        });

        commentVoList.forEach(commentVo -> {
            //遍历所有评论，查询评论是否有回复，如果有则继续构建属性评论菜单
            traverseToBuildTheTreeCommentMenu(commentList, commentVo);
        });


        PageHelper.startPage(pageDto.getPageSize(), pageDto.getPageNum());
        PageInfo pageInfo = new PageInfo(commentVoList);
        pageInfo.setSize(commentVoList.size());
        pageInfo.getList().forEach(i->{
            System.out.println(JSON.toJSONString(i));
        });
        return new Result().setCode(HTTPStatus.SUCCESS).setData(pageInfo);
    }

    /**
     * 构建树形评论菜单
     * @param commentList 所有评论
     * @param commentVoList 树形评论菜单
     */
    private void traverseToBuildTheTreeCommentMenu(List<Comment> commentList, CommentVo commentVo) {
        //遍历所有评论
        commentList.forEach(comment -> {
            //判断 回复评论编号 和 评论编号 是否一致
            if (comment.getParentId() == commentVo.getId()){
                //一致代表是 回复评论编号；将该评论装进树形评论菜单中
                CommentVo new_commentVo = new CommentVo();
                BeanUtils.copyProperties(comment,new_commentVo);
                commentVo.getChildren().add(new_commentVo);
                //继续遍历所有评论，判断这条回复评论是否有回复评论
                traverseToBuildTheTreeCommentMenu(commentList,new_commentVo);
            }
        });
    }

    /**
     * 发布评论
     * @param comment
     */
    @Override
    public Result issueComment(Comment comment) {
        //评论消息为空
        if (StringUtils.isEmpty(comment.getMessage())){
            return new Result().setCode(HTTPStatus.BAD_REQUEST).setMessage("评论消息不能为空");
        }
        //昵称为空
        if (StringUtils.isEmpty(comment.getNickname())){
            return new Result().setCode(HTTPStatus.BAD_REQUEST).setMessage("昵称不能为空");
        }
        comment.setCreateDate(LocalDateTime.now());

        comment.setParentId(StringUtils.isNotNull(comment.getParentId()) ? comment.getParentId():-1);

        commentMapper.insert(comment);
        String resultMessage = comment.getParentId()==-1?"评论发布成功":"评论回复成功";
        return new Result().setCode(HTTPStatus.SUCCESS).setMessage(resultMessage);
    }
}
